// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui;

import java.util.Map;
import java.util.Properties;

import org.dengues.model.project.ConnPropertyAttriType;
import org.dengues.model.project.ConnPropertySetType;
import org.dengues.ui.prefs.connectivity.EConnectivityProperties;
import org.eclipse.emf.common.util.EList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-4 qiang.zhang $
 * 
 */
public class PropertiesUiUtils {

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createPropertiesControls".
     * 
     * @param descComposite
     * @param properties
     */
    public static void createPropertiesControls(TabbedPropertySheetWidgetFactory factory, Composite descComposite, Map properties) {
        Control[] children = descComposite.getChildren();
        for (Control control : children) {
            control.dispose();
        }
        if (factory == null) {
            factory = new TabbedPropertySheetWidgetFactory();
        }
        GridData gridData;
        for (Object key : properties.keySet()) {
            String displayName = EConnectivityProperties.getDisplayNameFromId((String) key);
            if (displayName != null) {
                Label label2 = factory.createLabel(descComposite, displayName, SWT.NONE);
                gridData = new GridData();
                gridData.widthHint = 80;
                label2.setLayoutData(gridData);
                Text text2 = factory.createText(descComposite, (String) properties.get(key), SWT.BORDER);
                gridData = new GridData(GridData.FILL_HORIZONTAL);
                text2.setLayoutData(gridData);
                text2.setEnabled(false);
            }
        }
        descComposite.layout();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createPropertiesControls".
     * 
     * @param factory
     * @param descComposite
     * @param setType
     */
    public static void createPropertiesControls(TabbedPropertySheetWidgetFactory factory, Composite descComposite,
            ConnPropertySetType setType) {
        Properties properties = new Properties();
        EList<ConnPropertyAttriType> attributes = setType.getAttributes();
        for (ConnPropertyAttriType attr : attributes) {
            properties.put(attr.getName(), attr.getValue());
        }
        createPropertiesControls(factory, descComposite, properties);
    }
}
